package com.lily.aidouzixun.datacenter;


import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.util.Log;

import com.lily.aidouzixun.util.DataConverter;
import com.lily.aidouzixun.util.L;
import com.lily.aidouzixun.util.MyConfig;
import com.lily.aidouzixun.util.MyConstant;
import com.lily.aidouzixun.util.OKHttpClientManager;
import com.lily.aidouzixun.util.Validator;
import com.squareup.okhttp.Response;

import java.io.File;

import static com.lily.aidouzixun.util.MyConstant.ModifyAction;
import static com.lily.aidouzixun.util.MyConstant.RequireParams;
import static com.lily.aidouzixun.util.MyConstant.URLConstant;

/**
 * 远程服务端类
 * 封装请求参数，比如（地理位置，版本号等基本信息）
 * 调用网络基础层，得到Json格式数据返回
 * 所有方法为static方法，不能创建对象
 */
public class RemoteServer {
    private static final String TAG = "RemoteServer";

    public RemoteServer() {
        throw new UnsupportedOperationException("RemoteServer所有方法为static方法，不能创建对象");
    }

    /**
     * 检测版本更新
     * @return
     */
    public static String checkUpdate(Context appContext) {
        String checkUpdateUrl = URLConstant.INIT_URL + "?" + DataConverter.createParamByMap(getBaseParams(appContext));
        String result = OKHttpClientManager.getString(checkUpdateUrl);
        return result;
    }

    /**
     * 得到用户信息
     * @param urid
     * @return json数据
     */
    public static String getUserInfo(Context appContext, String urid) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        if (Validator.isEffective(urid)) {
            params.put(RequireParams.URID, urid);
        }
        String registerUrl = URLConstant.USERINFO_URL + "?";
        String resultJson = OKHttpClientManager.postString(registerUrl, params);
        return resultJson;
    }

    /**
     * 用户登录
     * @param loginName
     * @param loginPwd
     * @return json数据
     */
    public static String userLogin(Context appContext, String loginName, String loginPwd) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        if (Validator.isEffective(loginName)) {
            params.put(RequireParams.USER_NAME, loginName);
        }
        if (Validator.isEffective(loginPwd)) {   //密码md5加密
            params.put(RequireParams.PASSWORD, DataConverter.getMD5(loginPwd.getBytes()));
        }
        String registerUrl = URLConstant.LOGIN_URL + "?";
        String resultJson = OKHttpClientManager.postString(registerUrl, params);
        return resultJson;
    }

    /**
     * 用户注册
     * @param registerName
     * @param registerPwd
     * @return
     */
    public static String userRegister(Context appContext, String registerName, String registerPwd) {

        ArrayMap<String, String> params = getBaseParams(appContext);
        if (Validator.isEffective(registerName)) {
            params.put(RequireParams.USER_NAME, registerName);
        }
        if (Validator.isEffective(registerPwd)) {   //密码md5加密
            params.put(RequireParams.PASSWORD, DataConverter.getMD5(registerPwd.getBytes()));
        }
        String registerUrl = URLConstant.REGISTER_URL + "?";
        String resultJson = OKHttpClientManager.postString(registerUrl, params);
        return resultJson;
    }

    /**
     * 修改用户信息
     * @param appContext
     * @param action
     * @param modifyContent
     * @return
     */
    public static String modifyUserInfo(Context appContext, int action, String modifyContent){
        ArrayMap<String, String> params = getBaseParams(appContext);

        switch (action) {
            case ModifyAction.MODIFY_NICKNAME:  //修改昵称
                params.put(RequireParams.NICKNAME, modifyContent);
                break;
            case ModifyAction.MODIFY_PHONE: //修改手机号
                params.put(RequireParams.PHONE, modifyContent);
                break;
            case ModifyAction.MODIFY_EMAIL: //邮箱
                params.put(RequireParams.EMAIL, modifyContent);
                break;
            default:
                break;
        }
        String modifyUrl = URLConstant.MODIFY_USERINFO_URL + "?";
        String resultJson = OKHttpClientManager.postString(modifyUrl, params);
        return resultJson;
    }

    /**
     * 修改秘密
     * @param appContext
     * @param username
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public static String modifyPassword(Context appContext, String username, String oldPwd, String newPwd){
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.USER_NAME, username);
        params.put(RequireParams.PASSWORD, DataConverter.getMD5(oldPwd.getBytes()));
        params.put(RequireParams.NEW_PASSWORD, DataConverter.getMD5(newPwd.getBytes()));
        String modifyUrl = URLConstant.MODIFY_PASSWORD_URL + "?";
        String resultJson = OKHttpClientManager.postString(modifyUrl, params);
        return resultJson;
    }

    /**
     * 用户反馈
     * @param appContext
     * @param feedback
     * @return
     */
    public static String userFeedback(Context appContext, String feedback) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.FEEDBACK, feedback);
        String modifyUrl = URLConstant.FEEDBACK_URL + "?";
        String resultJson = OKHttpClientManager.postString(modifyUrl, params);
        return resultJson;
    }

    /**
     * 文章信息
     * @param appContext
     * @return
     */
    public static String getLongDistanceHome(Context appContext, int page) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.PAGE, page + "");
        String url = URLConstant.LONGDISTANCE_HOME_URL + "?" + DataConverter.createParamByMap(params);
        Log.e(TAG, "getLongDistanceHome: url:" + url);
        String resultJson = OKHttpClientManager.getString(url);
        return resultJson;
    }

    /**
     * @param appContext
     * @return
     */
    public static String getDiscussHome(Context appContext, int discussCategory, int pager) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.PAGE, pager +"");
        params.put(RequireParams.CATEGORY, discussCategory +"");
        String url = URLConstant.DISCUSS_HOME_URL + "?" + DataConverter.createParamByMap(params);
        Log.e(TAG, "getDiscussHome: url:" + url);
        String resultJson = OKHttpClientManager.getString(url);
        return resultJson;
    }

    /**
     * 得到收藏的动态信息
     * @param appContext
     * @return
     */
    public static String getDiscussCollect(Context appContext, int pager) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.PAGE, pager +"");
        String modifyUrl = URLConstant.DISCUSS_COLLECT_URL + "?" + DataConverter.createParamByMap(params);
        String resultJson = OKHttpClientManager.getString(modifyUrl);
        return resultJson;
    }

    /**
     * 发布动态
     * @param appContext
     * @param discussContent
     * @return
     */
    public static String publishDiscussText(Context appContext, String discussContent, int hasImage) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.DISCUSS_CONTENT, discussContent);
        if (hasImage == 1) {    //有照片
            params.put(RequireParams.HAS_IMAGE, "1");
        }
        String publishDiscussUrl = URLConstant.PUBLISH_DISCUSS_URL + "?";
        String resultJson = OKHttpClientManager.postString(publishDiscussUrl, params);
        return resultJson;
    }

    /**
     * 发布动态照片
     * @param appContext
     * @param imageName
     * @param photoPath
     * @return
     */
    public static String publishDiscussPhoto(Context appContext, String imageName, String photoPath) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        File file = new File(photoPath);
        String url = URLConstant.UPLOAD_IMAGE + "?" +"imagename=" + imageName;
        //todo
        Response response = OKHttpClientManager.postUpload(url, file, "discussPhoto", params);
        String result = null;
        try {
            if (response != null && response.code() == 200) {
                result = "200";
            } else {
                result = "255";
            }
           // result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除动态
     * @param appContext
     * @param did
     * @return
     */
    public static String deleteDiscuss(Context appContext, String did) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.DID, did);
        String publishDiscussUrl = URLConstant.DELETE_DISCUSS_URL + "?";
        String resultJson = OKHttpClientManager.postString(publishDiscussUrl, params);
        return resultJson;
    }

    /**
     * 上传头像
     * @param appContext
     * @param photoPath
     * @return
     */
    public static String uploadAvatar(Context appContext, String photoPath) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        File file = new File(photoPath);
        if (!Validator.isEffective(MyConfig.getUserId(appContext))) {
            return null;
        }
        if (!file.isFile()) {
            L.e(TAG, "avatar not is file!");
            return null;
        }
        String url = URLConstant.UPLOAD_AVATAR + "?" + RequireParams.URID + "=" + MyConfig.getUserId(appContext);
        //todo
        Response response = OKHttpClientManager.postUpload(url, file, "avatar", params);
        String result = null;
        try {
            if (response != null && response.code() == 200) {
                result = "200";
            } else {
                result = "255";
            }
            // result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 动态评论
     * @param appContext
     * @param did
     * @param comment
     * @return
     */
    public static String userDiscussComment(Context appContext, String did, String comment) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.DID, did);
        params.put(RequireParams.COMMENT, comment);
        String modifyUrl = URLConstant.DISCUSS_COMMENT_URL + "?";
        String resultJson = OKHttpClientManager.postString(modifyUrl, params);
        return resultJson;
    }
    /**
     * 动态评论
     * @param appContext
     * @param ldid
     * @param comment
     * @return
     */
    public static String longDistanceComment(Context appContext, String ldid, String comment) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.LDID, ldid);
        params.put(RequireParams.COMMENT, comment);
        String url = URLConstant.LONGDISTANCE_COMMENT_URL + "?";
        String resultJson = OKHttpClientManager.postString(url, params);
        return resultJson;
    }
    /**
     * 得到评论数据
     * @param appContext
     * @param did
     * @return
     */
    public static String commentData(Context appContext, String did, int pager) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.DID, did);
        params.put(RequireParams.PAGE, pager + "");
        String modifyUrl = URLConstant.COMMENT_LIST_URL + "?";
        String resultJson = OKHttpClientManager.postString(modifyUrl, params);
        return resultJson;
    }
    /**
     * 点赞动态
     * @param appContext
     * @param did
     * @return
     */
    public static String supportDiscuss(Context appContext, String did, int supportAction) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.DID, did);
        params.put(RequireParams.ACTION, supportAction + "");
        String url = URLConstant.SUPPORT_DISCUSS_URL + "?";
        String resultJson = OKHttpClientManager.postString(url, params);
        return resultJson;
    }
    /**
     * 收藏动态
     * @param appContext
     * @param did
     * @return
     */
    public static String collectDiscuss(Context appContext, String did, int action) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.DID, did);
        params.put(RequireParams.ACTION, action + "");
        String url = URLConstant.COLLECT_DISCUSS_URL + "?";
        String resultJson = OKHttpClientManager.postString(url, params);
        return resultJson;
    }
    /**
     * 反对动态
     * @param appContext
     * @param did
     * @return
     */
    public static String oppositionDiscuss(Context appContext, String did) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.DID, did);

        String url = URLConstant.OPPOSITION_DISCUSS_URL + "?";
        String resultJson = OKHttpClientManager.postString(url, params);
        return resultJson;
    }

    /**
     * 绑定QQ
     * @param appContext
     * @param openid
     * @param nickName
     * @param avatar
     * @return
     */
    public static String bindQQ(Context appContext, String openid, String nickName, String avatar) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.QQ_ID, openid);
        params.put(RequireParams.NICKNAME, nickName);
        params.put(RequireParams.AVATAR, avatar);
        String url = URLConstant.BIND_QQ_URL + "?";
        String resultJson = OKHttpClientManager.postString(url, params);
        return resultJson;
    }

    /**
     * 绑定手机号
     * @param appContext
     * @param phone
     * @return
     */
    public static String bindPhone(Context appContext, String phone) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        params.put(RequireParams.PHONE, phone);
        String url = URLConstant.BIND_PHONE_URL + "?";
        String resultJson = OKHttpClientManager.postString(url, params);
        return resultJson;
    }

    /**
     * 用户签到
     * @param appContext
     * @return
     */
    public static String userSign(Context appContext) {
        ArrayMap<String, String> params = getBaseParams(appContext);
        String url = URLConstant.USER_SIGN_URL + "?";
        String resultJson = OKHttpClientManager.postString(url, params);
        return resultJson;
    }

    /*********************************工具方法****************************************************/
    /**
     * 得到请求基础数据
     * @return
     */
    public static ArrayMap<String, String>  getBaseParams(Context appContext) {
        ArrayMap<String, String> baseParams = new ArrayMap<String, String>();
        baseParams.put("app_id", MyConstant.APP_ID);    //设备id
        baseParams.put("version_id", MyConstant.VERSION_ID);  //版本号
        baseParams.put("encrypt_did", "5a719fb440d9aeff87b86feccb218a76");  //加密字段
        //如果有用户名
        if (Validator.isEffective(MyConfig.getUserId(appContext))) {
            baseParams.put("urid", MyConfig.getUserId(appContext));
        }
        return baseParams;
    }

    /**
     * 提交crashlog
     * @param app
     * @param log
     * @return
     */
    public static String commitCrashLog(Context app, String log) {
        ArrayMap<String, String> map = getBaseParams(app);
        map.put("log", log);
        String url = URLConstant.CRASHREPORT_URL;
        L.i(TAG, "commitCrashLog,url=" + url + "?" + log);
        String resultJson = OKHttpClientManager.postString(url, map);
        return resultJson;
    }
}
